﻿/*
 * KLF Headless Browser
 * Written by Kris Oye <kristianoye@gmail.com>
 * Copyright (C) 2011-2012.  All rights reserved.
 */
namespace KLF.Lib.Browser.DOM.CSS.SAC.Selectors
{
    #region Using Statements

    using System;
    using KLF.Lib.Browser.DOM.HTML;

    #endregion

    public class ChildSelector : BinarySelector
    {
        public ChildSelector(Selector l, Selector r)
            : base(l, r)
        {
        }

        public override bool Matches(HTMLElement e)
        {
            if (Right == null || Left == null)
                throw new Exception("Attempted to evaluate poorly formated AdjacentSelector.");

            //  Left > Right
            if (Right.Matches(e) && e.Parent != null)
            {
                return Left.Matches(e.Parent as HTMLElement);
            }
            return false;
        }

        public override string ToString()
        {
            return String.Format("{0} > {1}", Left, Right);
        }
    }
}
